<template>
	<BaseContainer>
		<aplid-base-crud
			ref="crud"
			class="w-100"
			v-bind="{ ...baseConfig, ...$attrs }"
			@cell-click="handleCellClick"
			:operateFormate="operateFormate"
		>
			<template v-slot:addDialog="{ row, show }">
				<AddEditForm @refresh="handleRefresh" :key="show" :nowRowData="row"> </AddEditForm>
			</template>
			<template v-slot:editDialog="{ row, show }">
				<AddEditForm :subsidyApplyConfigId="row.id" @refresh="handleRefresh" v-if="show" :nowRowData="row"> </AddEditForm>
			</template>
		</aplid-base-crud>

		<AplidDialog v-model="visibleForm" width="65%" :rowData="nowRowData">
			<DetailConfig v-if="visibleForm" @refresh="handleRefresh"  :nowRowData="nowRowData"> </DetailConfig>
		</AplidDialog>
	</BaseContainer>
</template>

<script setup lang="ts">
import { baseConfig } from './config'
import AddEditForm from './AddEditForm/index.vue'
import DetailConfig from './DetailConfig/index.vue'
import AplidDialog from '@/views/instOperate/components/AplidDialog/index.vue'

const crud = ref()
const visibleForm = ref(false)
const nowRowData: any = ref({})
const commonType = ref('')

const handleRefresh = () => {
	nextTick(() => {
		crud.value.refreshChange({}, false)
	})
}
const handleCellClick = (row, column) => {
	nowRowData.value = row
	switch (column.property) {
		case 'policyEnable':
			visibleForm.value = row.policyRelList?.length > 0
			commonType.value = row[column.property] == 1 ? 'policyArchive' : ''
			break
		case 'tableEnable':
			visibleForm.value = row.tableRelList?.length > 0
			commonType.value = row[column.property] == 1 ? 'ApplyReport' : ''
			break
		case 'materialEnable':
			visibleForm.value = row.materialRelList?.length > 0
			commonType.value = row[column.property] == 1 ? 'MaterialForm' : ''
			break
		case 'dataEnable':
			visibleForm.value = row.dataRelList?.length > 0
			commonType.value = row[column.property] == 1 ? 'ApplyReport' : ''
			break
		default:
			break
	}
}

const operateFormate = (data, type) => {
	return new Promise((resolve, reject) => {
		if (type == 'detail') {
			nowRowData.value = data
			visibleForm.value = true
		} else {
			resolve(data)
		}
	})
}
</script>

<style scoped lang="scss">
:deep(.el-dialog) {
	--el-dialog-border-radius: 10px;
	--el-dialog-bg-color: rgba(238, 242, 255, 1);
}
:deep(.el-dialog__header) {
	padding: 0;
	margin: 0;
}
:deep(.el-dialog__body) {
	padding: 0;
}
</style>
